package com.qiangshi.tasks.service.impl;

import com.qiangshi.common.core.domain.entity.SysUser;
import com.qiangshi.common.utils.DateUtils;
import com.qiangshi.system.service.ISysUserService;
import com.qiangshi.taskClaim.domain.QsTaskClaim;
import com.qiangshi.taskClaim.mapper.QsTaskClaimMapper;
import com.qiangshi.taskClaim.service.IQsTaskClaimService;
import com.qiangshi.tasks.domain.QsProjectTasks;
import com.qiangshi.tasks.mapper.QsProjectTasksMapper;
import com.qiangshi.tasks.service.IQsProjectTasksService;
import com.qiangshi.tasksProgress.domain.QsTasksProgress;
import com.qiangshi.tasksProgress.mapper.QsTasksProgressMapper;
import com.qiangshi.tasksProgress.service.IQsTasksProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * 项目任务Service业务层处理
 *
 * @author qiangshi
 * @date 2022-02-12
 */
@Service
public class QsProjectTasksServiceImpl implements IQsProjectTasksService {
    @Autowired
    IQsProjectTasksService qsProjectTasksService;
    @Autowired
    IQsTasksProgressService qsTasksProgressService;
    @Autowired
    IQsTaskClaimService qsTaskClaimService;
    @Autowired
    private QsProjectTasksMapper qsProjectTasksMapper;
    @Autowired
    private QsTaskClaimMapper qsTaskClaimMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private QsTasksProgressMapper qsTasksProgressMapper;

    /**
     * 查询项目任务
     *
     * @param id 项目任务ID
     * @return 项目任务
     */
    @Override
    public QsProjectTasks selectQsProjectTasksById(Long id) {
        return qsProjectTasksMapper.selectQsProjectTasksById(id);
    }

    /**
     * 查询项目任务列表
     *
     * @param qsProjectTasks 项目任务
     * @return 项目任务
     */
    @Override
    public List<QsProjectTasks> selectQsProjectTasksList(QsProjectTasks qsProjectTasks) {
        return qsProjectTasksMapper.selectQsProjectTasksList(qsProjectTasks);
    }

    /**
     * 新增项目任务
     *
     * @param qsProjectTasks 项目任务
     * @return 结果
     */
    @Override
    public int insertQsProjectTasks(QsProjectTasks qsProjectTasks) {
        qsProjectTasks.setCreateTime(DateUtils.getNowDate());
        return qsProjectTasksMapper.insertQsProjectTasks(qsProjectTasks);
    }

    /**
     * 修改项目任务
     *
     * @param qsProjectTasks 项目任务
     * @return 结果
     */
    @Override
    public int updateQsProjectTasks(QsProjectTasks qsProjectTasks) {
        return qsProjectTasksMapper.updateQsProjectTasks(qsProjectTasks);
    }

    /**
     * 批量删除项目任务
     *
     * @param ids 需要删除的项目任务ID
     * @return 结果
     */
    @Override
    public int deleteQsProjectTasksByIds(Long[] ids) {
        return qsProjectTasksMapper.deleteQsProjectTasksByIds(ids);
    }

    /**
     * 删除项目任务信息
     *
     * @param id 项目任务ID
     * @return 结果
     */
    @Override
    public int deleteQsProjectTasksById(Long id) {
        return qsProjectTasksMapper.deleteQsProjectTasksById(id);
    }

    /**
     * 认领任务
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int claimQsProjectTasks(Long id, Long userId) {
        QsProjectTasks qsProjectTasks = qsProjectTasksMapper.selectQsProjectTasksById(id);
        if (Objects.isNull(qsProjectTasks) || qsProjectTasks.getTasksType() != 1) {
            return 0;
        }
        SysUser pUser = sysUserService.selectUserById(qsProjectTasks.getPublisherId());
        SysUser cUser = sysUserService.selectUserById(userId);
        QsTaskClaim qsTaskClaim = new QsTaskClaim(id
                , qsProjectTasks.getTasksNum(), qsProjectTasks.getTasksName(), userId, qsProjectTasks.getPublisherId(), pUser.getNickName(), cUser.getNickName(), DateUtils.getNowDate()
                , qsProjectTasks.getReleaseScore(), qsProjectTasks.getClaimScore());
        qsTaskClaim.setCreateTime(DateUtils.getNowDate());
        qsTaskClaimMapper.insertQsTaskClaim(qsTaskClaim);
        QsTasksProgress qsTasksProgress = new QsTasksProgress(
                qsTaskClaim.getId(), id, qsProjectTasks.getTasksName(), qsProjectTasks.getTasksNum(), userId, cUser.getNickName(), "暂无进度", 0
        );
        qsTasksProgress.setCreateTime(DateUtils.getNowDate());
        qsTasksProgressMapper.insertQsTasksProgress(qsTasksProgress);
        qsProjectTasks.setClaimerId(userId);
        qsProjectTasks.setTasksType(2);
        return qsProjectTasksMapper.updateQsProjectTasks(qsProjectTasks);
    }

    /**
     * 提报任务进度
     *
     * @param qsTasksProgress
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int reportQsTasksProgress(QsTasksProgress qsTasksProgress) {
        QsProjectTasks qsProjectTasks = qsProjectTasksMapper.selectQsProjectTasksById(qsTasksProgress.getTasksId());
        if (Objects.isNull(qsProjectTasks) || qsProjectTasks.getTasksType() != 2) {
            return 0;
        }
        if (!(qsTasksProgress.getProgressNum() > 0 && qsTasksProgress.getProgressNum() <= 100)) {
            return 0;
        }
        SysUser cUser = sysUserService.selectUserById(qsTasksProgress.getClaimerId());
        QsTaskClaim qsTaskClaim = new QsTaskClaim();
        qsTaskClaim.setClaimerId(qsTasksProgress.getClaimerId());
        qsTaskClaim.setTasksId(qsTasksProgress.getTasksId());
        qsTaskClaim = qsTaskClaimMapper.getQsTaskClaimOne(qsTaskClaim);
        qsTasksProgress.setTasksClaimId(qsTaskClaim.getId());
        qsTasksProgress.setTasksName(qsProjectTasks.getTasksName());
        qsTasksProgress.setTasksNum(qsProjectTasks.getTasksNum());
        qsTasksProgress.setClaimerName(cUser.getNickName());
        qsTasksProgress.setCreateTime(DateUtils.getNowDate());
        return qsTasksProgressMapper.insertQsTasksProgress(qsTasksProgress);
    }

    /**
     * 查询需要修改为完成的项目任务列表
     *
     * @return 项目任务集合
     */
    @Override
    public List<QsProjectTasks> selectCompletionQsProjectTasksList() {
        return qsProjectTasksMapper.selectCompletionQsProjectTasksList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void taskCompletionProcessing() {
        List<QsProjectTasks> qsProjectTasksList = qsProjectTasksService.selectCompletionQsProjectTasksList();
        if(qsProjectTasksList == null || qsProjectTasksList.isEmpty()){
            return;
        }
        QsTaskClaim qsTaskClaim = new QsTaskClaim();
        qsTaskClaim.setTasksIds(qsProjectTasksList.stream().map(QsProjectTasks::getId).collect(toList()));
        List<QsTaskClaim> qsTaskClaimList = qsTaskClaimService.selectQsTaskClaimList(qsTaskClaim);
        Map<Long, QsTaskClaim> qsTaskClaimMap = qsTaskClaimList.stream().collect(Collectors.groupingBy(QsTaskClaim::getTasksId, Collectors.collectingAndThen(toList(), value -> value.get(0))));
        List<SysUser> sysUserList = sysUserService.selectUserAllList();
        Map<Long, SysUser> sysUserMap = sysUserList.stream().collect(Collectors.groupingBy(SysUser::getUserId, Collectors.collectingAndThen(toList(), value -> value.get(0))));
        List<QsTasksProgress> qsTasksProgressList = new ArrayList<>();
        for (QsProjectTasks qsProjectTasks : qsProjectTasksList) {
            qsProjectTasks.setTasksType(3);
            QsTasksProgress qsTasksProgress = new QsTasksProgress(
                    qsTaskClaimMap.get(qsProjectTasks.getId()).getId(), qsProjectTasks.getId(), qsProjectTasks.getTasksName(),
                    qsProjectTasks.getTasksNum(), qsProjectTasks.getClaimerId(), sysUserMap.get(qsProjectTasks.getClaimerId()).getNickName()
                    , "预计时间已到达，任务完成终止", 100
            );
            qsTasksProgress.setCreateTime(DateUtils.getNowDate());
            qsTasksProgressList.add(qsTasksProgress);
        }
        qsTasksProgressService.saveBatch(qsTasksProgressList);
        qsProjectTasksService.updateBatchById(qsProjectTasksList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBatchById(List<QsProjectTasks> qsProjectTasksList) {
        for (QsProjectTasks qsProjectTasks : qsProjectTasksList) {
            if (qsProjectTasksMapper.updateQsProjectTasks(qsProjectTasks) < 1) {
                return false;
            }
        }
        return true;
    }
}
